﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>入库单编辑</title>
    <script src="/Apm/js/CMSEdit.js"></script>
    <style>

    </style>
</head>
<body>
    <div class="page">
        <div id="title" class="title">

        </div>
        <div class="layui-form layui-form-pane edit-form">
            <div class="layui-form-item">
                <label class="layui-form-label">仓库</label>
                <div class="layui-input-block">
                    <select id="WareHouseGuidList" name="WareHouseGuid" required lay-verify="required" lay-filter="WareHouseGuidList">
                        <option value="">请选择仓库</option>

                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">入库时间</label>
                <div class="layui-input-block">
                    <input id="AddTime" type="text" name="AddTime" required lay-verify="required" placeholder="请选择时间" autocomplete="off" class="layui-input">

                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">应付款</label>
                <div class="layui-input-block">
                    <input id="Yf" type="text" name="Yf" required lay-verify="required" autocomplete="off" class="layui-input" disabled>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">实付款</label>
                <div class="layui-input-block">
                    <input id="Sf" type="text" name="Sf" required lay-verify="required|number" onchange="FnWeifu()" placeholder="请填写付款金额" autocomplete="off" class="layui-input">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">未付款</label>
                <div class="layui-input-block">
                    <input id="Wf" type="text" name="Wf" required lay-verify="required" autocomplete="off" class="layui-input" disabled>
                </div>
            </div>

            <button id="submit" lay-submit lay-filter="submit" style="display:none;"></button>
        </div>

        <div class="edit-submit">
            <button id="truebutton" class="layui-btn layui-btn-lg">提交</button>
        </div>
    </div>
</body>
</html>
<script>
    layui.use(['layer', 'form', 'laydate'], function () {
        var layer = layui.layer,
            form = layui.form,
            laydate = layui.laydate;
        
        //执行一个laydate实例
        laydate.render({
            elem: '#AddTime' //指定元素
        });

        var type = MyPublic.getUrlParam('type');
        var PurchaseGuid = MyPublic.getUrlParam('PurchaseGuid');//采购单guid
        var StorageGuid = MyPublic.getUrlParam('StorageGuid');//入库单guid


        switch (type) {
            case 'add':
                $("#title").html('添加入库单');

                break;
            case 'edit':
                $("#title").html('编辑入库单');
                //编辑时候仓库不能重新选择（因为这会影响库存）
                $("#WareHouseGuidList").attr('disabled', true);
                break;
        }
        GetTotalYF(PurchaseGuid);
        GetCKList(type, StorageGuid);
        $("#truebutton").click(function () {
            $("#submit").click();
        })

        //添加或修改
        form.on('submit(submit)', function (data) {
            //console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            var json = data.field;
            json.PurchaseGuid = PurchaseGuid;
            //console.log(json);
            ajaxpost({
                url: 'api/FriendFlower/OrderStorageCreate?Token=' + MyPublic.getToken(),
                data: json,
                success: function (data) {
                    data = JSON.parse(data);
                    if (data.code === "10000") {
                        //当你在iframe页面关闭自身时
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    }
                    else {
                        layer.msg(data.msg);
                    }
                }
            })

            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


    });
    /**
     * 获取采购单的应付总金额
     * @param guid
     */
    function GetTotalYF(guid) {
        ajaxget({
            url: 'api/FriendFlower/GetPurchaseTotalPrice?Token=' + MyPublic.getToken()+'&Guid='+guid,
            success: function (data) {
                $("#Yf").val(data);
            }
        })
    }

    /**
     * 仓库数据绑定
     * @param type
     * @param id
     */
    function GetCKList(type,id) {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;

            ajaxget({
                url: 'api/FriendFlower/WareHouseList?Token=' + MyPublic.getToken(),
                success: function (data) {
                    var lis = [];
                    layui.each(data, function (index, item) {
                        lis.push('<option value="' + item.Guid + '">' + item.Name + '</option>');
                    })
                    var datahtml = lis.join('');

                    $("#WareHouseGuidList").append(datahtml);
                    form.render('select');
                    if (type == "edit") {
                        BindDataModel(id);
                    }
                }
            })
        });
    }
    
    function BindDataModel(guid) {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;

            ajaxget({
                url: 'api/MethodPublic/UserInfoModel?UserCode=' + guid,
                success: function (data) {
                    $("#Name").val(data.Name);
                    $("#Phone").val(data.Phone);
                    $("#LoginName").val(data.Phone);
                    $("#GroupList").val(data.GroupCode);
                    if (data.Sex === 0) {
                        $("#Sex").attr('checked', false)
                    }
                    if (data.State === 0) {
                        $("#State").attr('checked', false)
                    }
                    form.render('select');
                    form.render('checkbox');
                }
            })
        });
    }
    /**
     * 未付款计算
     */
    function FnWeifu() {
        layui.use('layer', function () {
            var layer = layui.layer;
                
            var yf = $("#Yf").val();//应付款
            var sf = $("#Sf").val();//实付款
            var wf = yf - sf;
            if (wf < 0) {
                $("#Wf").val('');
                layer.msg("实付金额不能大于应付金额");
            }
            else {
                $("#Wf").val(wf);
            }
            
        });
        
    }
</script>